package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


// 购物车
@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    //添加购物车
    @Override
    public Cart add(Cart cartParam) {
        Long userId = UserHolder.get().getId();

        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        //查询用户id   select* from shopping_cart  where user_id=?  DishId=？ | Setmeal_id =？
        qw.eq(Cart::getUserId, userId);
        //查菜品或者套餐id
        qw.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());
        qw.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId());
        Cart cart = cartMapper.selectOne(qw);

        if (cart != null) {

            cart.setNumber(cart.getNumber() + 1);
            cartMapper.updateById(cart);

        } else {

            cart = cartParam;
            cart.setUserId(userId); // 用户id
            cart.setNumber(1); // 购买数量
            cart.setCreateTime(new Date()); // 添加时间
            cartMapper.insert(cart);

        }
        return cart;
    }

    // 查询购物车
    @Override
    public List<Cart> list() {
        //select * from  shopping_cart where user_id =？
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        List<Cart> carts = cartMapper.selectList(qw);
        return carts;
    }

    @Override
    public void clear() {
        // delete * from  shopping_cart where user_id =？
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        cartMapper.delete(qw);
    }

    @Override
    public Cart reduce(Cart cartParam) {
        //获得用户id
        Long userId = UserHolder.get().getId();
        //根据用户id查询购物车
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        //查询用户id   select* from shopping_cart  where user_id=?  DishId=？ | Setmeal_id =？
        qw.eq(Cart::getUserId, userId);
        //查菜品或者套餐id
        qw.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());
        qw.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId());
        Cart cart = cartMapper.selectOne(qw);
          //如果大于0就减一
        if (cart.getNumber() != 0) {
            cart.setNumber(cart.getNumber() - 1);
            cartMapper.updateById(cart);

        }

        if(cart.getNumber() == 0){
            cartMapper.deleteById(cart);

        }

        return cart;
    }
}
